<template>
  <el-dialog :title="'新增卡密'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="卡密数量" prop="cdkeynum">
        <el-input type="number" v-model="dataForm.cdkeynum" placeholder="卡密数量"></el-input>
      </el-form-item>
      <el-form-item label="卡密类型" prop="cdkType">
        <el-select v-model="dataForm.cdkType" style="width:100%" filterable placeholder="卡密类型">
          <el-option v-for="item in cdkTypeList" :key="item.code" :label="item.value" :value="item.code" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期类型" prop="cdkDateType">
        <el-select v-model="dataForm.cdkDateType" style="width:100%" filterable placeholder="日期类型">
          <el-option v-for="item in dateTypeList" :key="item.code" :label="item.value" :value="item.code" :disabled="item.disabled">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          cdkeynum: '',
          cdkType: '',
          cdkDateType: ''
        },
        dateTypeList: [],
        cdkTypeList: [],
        dataRule: {
          cdkeynum: [{
            required: true,
            message: '卡密不能为空',
            trigger: 'blur'
          }],
          cdkType: [{
            required: true,
            message: 'cdk类型',
            trigger: 'blur'
          }],
          cdkDateType: [{
            required: true,
            message: '卡密日期类型',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(cdkTypeList, dateTypeList) {
        this.dateTypeList = dateTypeList
        this.cdkTypeList = cdkTypeList
        this.visible = true
        this.$refs['dataForm'].resetFields()
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cards/amgcdkcards/batchAdd`),
              method: 'post',
              params: this.$http.adornParams({
                'cdkeynum': this.dataForm.cdkeynum,
                'cdkType': this.dataForm.cdkType,
                'cdkDateType': this.dataForm.cdkDateType,

              })
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }

</script>
